package com.imarad.bio.socket.common;

import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hhu 【huan.hu@cnambition.com】
 * @version com.imarad.demo, v 0.1 2018/1/30 13:41 hhu Exp $$
 */
public class ClientUtils {

    private final static String LOGIN_COMMAND = "login";

    private final static String EXIT = "exit";

    /**
     * @param pos byte转化起始位
     */
    public static int bytesToInt(byte[] bytes, int pos) {
        return bytes[pos + 3] & 0xFF | (bytes[pos + 2] & 0xFF) << 8 | (bytes[pos + 1] & 0xFF) << 16
                | (bytes[pos] & 0xFF) << 24;
    }

    /**
     * @param pos byte转化起始位
     */
    public static long bytesToLong(byte[] bytes, int pos) {
        long l;
        l = bytes[pos];
        l &= 0xff;
        l |= ((long) bytes[pos + 1] << 8);
        l &= 0xffff;
        l |= ((long) bytes[pos + 2] << 16);
        l &= 0xffffff;
        l |= ((long) bytes[pos + 3] << 24);
        l &= 0xffffffffL;
        l |= ((long) bytes[pos + 4] << 32);
        l &= 0xffffffffffL;
        l |= ((long) bytes[pos + 5] << 40);
        l &= 0xffffffffffffL;
        l |= ((long) bytes[pos + 6] << 48);
        l &= 0xffffffffffffffL;
        l |= ((long) bytes[pos + 7] << 56);
        return (long) Double.longBitsToDouble(l);
    }

    /**
     * @param pos byte转化起始位
     */
    public static short bytesToShort(byte[] b, int pos) {
        short s0 = (short) (b[pos] & 0xff);
        short s1 = (short) (b[pos + 1] & 0xff);
        s1 <<= 8;
        return (short) (s0 | s1);
    }

    /**
     *
     * @param number
     * @return
     */
    public static byte[] shortToBytes(short number) {
        int temp = number;
        byte[] bytes = new byte[2];
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = Integer.valueOf(temp & 0xff).byteValue(); // 将最低位保存在最低位
            temp = temp >> 8; // 向右移8位
        }
        return bytes;
    }

    public static byte[] intToBytes(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n >> 24 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[3] = (byte) (n & 0xff);
        return b;
    }

    public static byte[] double2Bytes(double d) {
        long value = Double.doubleToRawLongBits(d);
        byte[] byteRet = new byte[8];
        for (int i = 0; i < 8; i++) {
            byteRet[i] = (byte) ((value >> 8 * i) & 0xff);
        }
        return byteRet;
    }


    private static final String REX_NET_TYPE = "localhost|127.0.0.1|"
            + "((192\\.168|172\\.([1][6-9]|[2]\\d|3[01]))"
            + "(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){2}|"
            + "^(\\D)*10(\\.([2][0-4]\\d|[2][5][0-5]|[01]?\\d?\\d)){3})";

    /**
     * 判断是否属于内网ip
     *
     * @param ip IP地址
     * @return 判断结果
     */
    public static boolean isInternet(String ip) {
        return !matchIgnoreCase(ip, REX_NET_TYPE);
    }

    private static boolean match(String srcStr, String regEx, Pattern pattern) {
        Matcher matcher = pattern.matcher(srcStr);
        // 字符串是否与正则表达式相匹配
        boolean isMatch = matcher.matches();
        Log.d("ClientUtils", ">>>匹配字段：" + srcStr + ", 匹配规则：" + regEx + ", 是否匹配：" + isMatch);
        return isMatch;
    }

    /**
     * 正则匹配 (不区分大小写)
     *
     * @param srcStr 匹配字符串
     * @param regEx  正则规则
     * @return 是否匹配规则
     */
    public static boolean matchIgnoreCase(String srcStr, String regEx) {
        Pattern pattern = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        return match(srcStr, regEx, pattern);
    }
}